package com.mama100.rpc.mdp.service.finagle;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.mama100.common.finagle.util.FinagleRPC;
import com.mama100.common.finagle.util.RpcUtil;
import com.mama100.rpc.mdp.MdpInoutUtil;
import com.mama100.rpc.mdp.constant.ResponseCode;
import com.mama100.rpc.mdp.thrift.inout.BindCustTerminalRequest;
import com.mama100.rpc.mdp.thrift.inout.GetCustTerminalByTerminalCodeRequest;
import com.mama100.rpc.mdp.thrift.inout.GetCustTerminalByTerminalCodeResponse;
import com.mama100.rpc.mdp.thrift.inout.GetCustTerminalRequest;
import com.mama100.rpc.mdp.thrift.inout.GetCustTerminalResponse;
import com.mama100.rpc.mdp.thrift.inout.TCustTerminal;
import com.mama100.rpc.mdp.thrift.inout.UnBindCustTerminalRequest;
import com.mama100.rpc.mdp.thrift.inout.common.BaseResponse;
import com.twitter.util.ExecutorServiceFuturePool;
import com.twitter.util.Function0;
import com.twitter.util.Future;

@FinagleRPC
@Service("custTerminalService")
public class CustTerminalService implements com.mama100.rpc.mdp.thrift.CustTerminalService.ServiceIface {
	protected Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ExecutorServiceFuturePool futurePool;

	@Resource(name = "cpmsJdbcTemplate")
	JdbcTemplate jdbcTemplate;

	@Override
	public Future<BaseResponse> bind(BindCustTerminalRequest req) {

		return futurePool.apply(new Function0<BaseResponse>() {
			@Override
			public BaseResponse apply() {
				BaseResponse resp = new BaseResponse();

				if (req.getBaseReq().getSeqNo() == null) {
					resp.setSeqNo(RpcUtil.genSeqNo());
				} else {
					resp.setSeqNo(req.getBaseReq().getSeqNo());
				}
				log.info("receive:" + req.toString());
				// dbservice save
				try {
					long customerId = req.getCustomerId();
					long TerminalCode = req.getTerminalCode();
					String fromSystem = req.getFromSystem();
					String updateBy = req.getUpdateBy();
					Date updateTime = new Date();

					// update
					int updated = jdbcTemplate.update(
							"update  cpms_cust_terminal set  update_by =  ? , update_time=?,from_system=? where customer_id =? and terminal_code = ? ",
							updateBy, updateTime, fromSystem, customerId, TerminalCode);
					if (updated == 0) {
						// insert
						jdbcTemplate
								.update("insert into cpms_cust_terminal (id,customer_id,terminal_code,create_time,create_by,update_time,update_by,from_system) values ( CPMS_CUST_TERMINAL_SEQ.nextval,?,?,?,?,?,?,?  )",
										customerId, TerminalCode, updateTime, updateBy, updateTime, updateBy, fromSystem);

					}
				} catch (Exception e) {
					log.error("error:", e);
					resp.setRespCode(ResponseCode.SERVER_ERROR.getCode());
					resp.setRespDesc(ResponseCode.SERVER_ERROR.getDesc());
					log.info("return:" + resp);
					return resp;
				}
				resp.setRespCode(ResponseCode.SUCCESS.getCode());
				resp.setRespDesc(ResponseCode.SUCCESS.getDesc());
				log.info("return:" + resp.toString());
				return resp;
			}

		});

	}

	@Override
	public Future<BaseResponse> unbind(UnBindCustTerminalRequest req) {
		return futurePool.apply(new Function0<BaseResponse>() {
			@Override
			public BaseResponse apply() {
				BaseResponse resp = new BaseResponse();

				if (req.getBaseReq().getSeqNo() == null) {
					resp.setSeqNo(RpcUtil.genSeqNo());
				} else {
					resp.setSeqNo(req.getBaseReq().getSeqNo());
				}
				log.info("receive:" + req.toString());
				try {
					long customerId = req.getCustomerId();
					long TerminalCode = req.getTerminalCode();
					jdbcTemplate.update("delete from cpms_cust_terminal where customer_id= ? and terminal_code = ? ", customerId, TerminalCode);
				} catch (Exception e) {
					log.error("error:", e);
					resp.setRespCode(ResponseCode.SERVER_ERROR.getCode());
					resp.setRespDesc(ResponseCode.SERVER_ERROR.getDesc());
					log.info("return:" + resp);
					return resp;
				}
				resp.setRespCode(ResponseCode.SUCCESS.getCode());
				resp.setRespDesc(ResponseCode.SUCCESS.getDesc());
				log.info("return:" + resp );
				return resp;
			}

		});
	}

	@Override
	public Future<GetCustTerminalResponse> getCustTerminal(GetCustTerminalRequest req) {
		return futurePool.apply(new Function0<GetCustTerminalResponse>() {
			@Override
			public GetCustTerminalResponse apply() {

				GetCustTerminalResponse resp = MdpInoutUtil.getGetCustTerminalResponse(RpcUtil.genSeqNo());
				BaseResponse base = resp.getBaseResp();
				if (req.getBaseReq().getSeqNo() != null) {
					base.setSeqNo(req.getBaseReq().getSeqNo());
				}
				log.info("receive:" + req.toString());
				try {
					long customerId = req.getCustomerId();
					RowMapper<TCustTerminal> rowMapper = new RowMapper<TCustTerminal>() {
						@Override
						public TCustTerminal mapRow(ResultSet rs, int rowNum) throws SQLException {
							TCustTerminal row = new TCustTerminal();
							row.setTerminalCode(rs.getLong("terminal_code"));
							row.setUpdateBy(rs.getString("update_by"));
							row.setUpdateTime(rs.getDate("update_time").getTime());
							row.setFromSystem(rs.getString("from_system"));
							return row;
						}
					};
					String sql = "select  customer_id,terminal_code,update_time,update_by,from_system from cpms_cust_terminal where customer_id = ? ";
					List<TCustTerminal> value = jdbcTemplate.query(sql, new Object[] { customerId }, rowMapper);
					resp.setValue(value);
				} catch (Exception e) {
					log.error("error:", e);
					base.setRespCode(ResponseCode.SERVER_ERROR.getCode());
					base.setRespDesc(ResponseCode.SERVER_ERROR.getDesc());
					log.info("return:" + resp);
					return resp;
				}
				base.setRespCode(ResponseCode.SUCCESS.getCode());
				base.setRespDesc(ResponseCode.SUCCESS.getDesc());
				log.info("return:" +resp );
				return resp;
			}

		});

	}

	@Override
	public Future<GetCustTerminalByTerminalCodeResponse> getCustTerminalByTerminalCode(GetCustTerminalByTerminalCodeRequest req) {
		return futurePool.apply(new Function0<GetCustTerminalByTerminalCodeResponse>() {
			@Override
			public GetCustTerminalByTerminalCodeResponse apply() {

				GetCustTerminalByTerminalCodeResponse resp = MdpInoutUtil.getGetCustTerminalByTerminalCodeResponse(RpcUtil.genSeqNo());
				BaseResponse base = resp.getBaseResp();
				if (req.getBaseReq().getSeqNo() != null) {
					base.setSeqNo(req.getBaseReq().getSeqNo());
				}
				log.info("receive:" + req.toString());
				try {
					long terminalId = req.getTerminalCode();
					RowMapper<TCustTerminal> rowMapper = new RowMapper<TCustTerminal>() {
						@Override
						public TCustTerminal mapRow(ResultSet rs, int rowNum) throws SQLException {
							TCustTerminal row = new TCustTerminal();
							row.setTerminalCode(rs.getLong("terminal_code"));
							row.setUpdateBy(rs.getString("update_by"));
							row.setUpdateTime(rs.getDate("update_time").getTime());
							row.setFromSystem(rs.getString("from_system"));
							return row;
						}
					};
					String sql = "select customer_id,Terminal_code,update_time,update_by,from_system from cpms_cust_terminal where Terminal_code = ? ";
					List<TCustTerminal> value = jdbcTemplate.query(sql, new Object[] { terminalId }, rowMapper);
					resp.setValue(value);
				} catch (Exception e) {
					log.error("error:", e);
					base.setRespCode(ResponseCode.SERVER_ERROR.getCode());
					base.setRespDesc(ResponseCode.SERVER_ERROR.getDesc());
					log.info("return:" + resp);
					return resp;
				}
				base.setRespCode(ResponseCode.SUCCESS.getCode());
				base.setRespDesc(ResponseCode.SUCCESS.getDesc());
				log.info("return:" +resp );
				return resp;
			}

		});
	}
}
